package com.sea.finance.service.impl;

import com.codecrane.core.web.BatchImportUtils;
import com.sea.finance.entity.VtFeeMatch;
import com.sea.finance.mapper.VtFeeMatchMapper;
import com.sea.finance.service.VtFeeMatchService;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;


/**
 * @author 陈鸿飞
 */
@Service
public class VtFeeMatchServiceImpl implements VtFeeMatchService {

    @Autowired
    private VtFeeMatchMapper vtFeeMatchMapper;

    @Override
    public Integer save(VtFeeMatch vtFeeMatch) {
        return vtFeeMatchMapper.insert(vtFeeMatch);
    }

    @Override
    public List<VtFeeMatch> findByCnd(VtFeeMatch vtFeeMatch) {
        return vtFeeMatchMapper.findByCnd(vtFeeMatch);
    }

    @Override
    public void importDetail(InputStream inputStream, String fileName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<VtFeeMatch> list = BatchImportUtils.iteratorForVtFeeMatch(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少新增一条明细！");
            } else {
                for (VtFeeMatch vt : list) {
                    try {
                        save(vt);
                    }catch (DuplicateKeyException e){
                        usave(vt);
                    }

                }
            }
        }
    }

    @Override
    public List<VtFeeMatch> feeMatch(VtFeeMatch vtFeeMatch) {
        return vtFeeMatchMapper.feeMatch(vtFeeMatch);
    }

    @Override
    public Integer usave(VtFeeMatch vtFeeMatch) {
        return vtFeeMatchMapper.update(vtFeeMatch);
    }

    @Override
    public List<VtFeeMatch> findByFeeType() {
        return vtFeeMatchMapper.findByFeeType();
    }
}
